<template>
  <el-dialog
    :title="id?'修改':'添加'"
    :visible.sync="dialogVisible"
    width="500px"
    :show="show"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @open='open'
  >
    <el-form ref="form" :model="info" label-width="90px" :rules="rules"
             v-loading="infoLoading">
      <div class="el-dialog-div form-scrollbar">
        <el-form-item label="预览" v-if="info.color && info.name">
          <el-tag
            :style="{'background-color':info.color,'border-color':info.color}"
            effect="dark">
            {{ info.name }}
          </el-tag>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input type="text" v-model="info.name"
                    placeholder="请填写标签名称" maxlength="255"/>
        </el-form-item>

        <el-form-item label="颜色" prop="color">
          <el-color-picker v-model="info.color"></el-color-picker>
        </el-form-item>

      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-row>
        <el-col :span="12" class="btnEdit">
          <el-button :disabled="infoLoading" type="primary" @click="subInfo">保存</el-button>
        </el-col>
        <el-col :span="12" class="btnEdit">
          <el-button @click="handleClose">取消</el-button>
        </el-col>
      </el-row>
    </div>
  </el-dialog>
</template>

<script>
import { createLabel, getLabel, getList, updateLabel } from '@/api/customer/label'
import { createArticle } from '@/api/articles'

export default {
  name: 'edit',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    id: '',
    closeAction: ''
  },
  data() {
    return {
      dialogVisible: this.show,
      isChange: false,
      initForm: false,
      // 页面元素
      info: {},
      tempData: {
        name: '',
        color: '',
      },
      infoLoading: true,
      rules: {
        //element form表单元素验证
        name: [{required: true, message: '请填写名称', trigger: 'blur'}],
        color: [{required: true, message: '请选择颜色', trigger: 'blur'}],
      },
    }
  },
  methods: {
    async open() {
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
      if (this.id != '') {
        const {data} = await getLabel(this.id)
        this.info = data
        console.log(data);
      } else {
        this.resetTempData()
        this.info = this.tempData
      }
      this.initForm = true
      setTimeout(() => {
        this.infoLoading = false
      }, 0.5 * 1000)
    },
    subInfo() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          this.infoLoading = true
          const data = this.info
          if (this.id != '') {
           await updateLabel(this.id, data).then((res) => {
             this.$notify({
               title: '成功',
               message: '更新成功',
               type: 'success',
               duration: 2000
             })
             this.$emit('closeAction')
           }).catch(() => {
             setTimeout(() => {
               this.infoLoading = false
             }, 0.5 * 1000)
           })
          } else {
           await createLabel(data).then((res) => {
             this.$notify({
               title: '成功',
               message: '创建成功',
               type: 'success',
               duration: 2000
             })
             this.$emit('closeAction')
           }).catch(() => {
             setTimeout(() => {
               this.infoLoading = false
             }, 0.5 * 1000)
           })
          }

        }
      })
    },
    resetTempData() {
      this.tempData = {
        name: '',
        color: ''
      }
    },
    handleClose(done) {
      if (this.isChange) {
        this.$confirm('您有未保存的修改，确定关闭此页吗？','提示', {
          type: 'warning'
        })
          .then(_ => {
            this.$emit('update:show', false)
          })
          .catch(_ => {
          })
      } else {
        this.$emit('update:show', false)
      }
    }
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    },
    tempData: {
      handler: function() {
        if (this.initForm) {
          this.isChange = false
          this.initForm = false
        } else {
          this.isChange = true
        }
      },
      deep: true
    }
  }
}
</script>

<style scoped>
.form-scrollbar {
  height: 200px !important;
}
</style>
